#!/usr/bin/env python  
# -*- coding: utf-8 -*-  
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
from os.path import join as pjoin
import sys,re,urllib2,urllib,time,os,json,codecs,csv,smtplib

type = sys.getfilesystemencoding() 
reload(sys)  
sys.setdefaultencoding('utf8')


class Youtube_video:
    #开始方法
    def __init__(self):
        self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
        self.headers    = { 'User-Agent' : self.user_agent }
        current_path = os.path.abspath(__file__)
        self.father_path = os.path.abspath(os.path.dirname(current_path) + os.path.sep + ".") 
        self.channels = []
        self.channel_video = []
        self.channel_viewCount = ''

    def start(self,channel_id):
        self.channel_id = channel_id
        # UC02UVoPQeVi5yVJAMmn1Fmw,UC-MByr5LRnWmVN94Ija4XJg
        # 获取 go车志 频道数据
        self.getChannelData(channel_id)
        
        # 获取频道影片列表 items 数据
        self.getChannelItems(channel_id)

        # # 获取影片数据
        self.getVideoData()

        ## 數據處理
        if(self.channel_id == 'UCW92-RPH4xq1YqVbON4D0fg'):
            self.getJson()
            self.sendEmail()

    def getChannelData(self,channel_array):
        url             = 'https://www.googleapis.com/youtube/v3/channels'
        part            = 'snippet,contentDetails,statistics'
        key             = 'AIzaSyBS8p-aiWmI6jeCFvRZTYKgO5AkqrAXmkA'
        channel_url     = url + '?id='+ channel_array +'&part='+ part +'&key='+ key
        request         = urllib2.Request(channel_url, headers = self.headers)
        response        = urllib2.urlopen(request).read()
        channel_msg     = json.loads(response)

        self.channel_viewCount =  channel_msg['items'][0]['statistics']['subscriberCount']
        self.channels = channel_msg
        

    def getChannelItems(self,channel_array):
        url             = 'https://www.googleapis.com/youtube/v3/search'
        part            = 'snippet,id'
        key             = 'AIzaSyBS8p-aiWmI6jeCFvRZTYKgO5AkqrAXmkA'
        channel_url     = url +'?channelId='+ channel_array +'&part='+ part +'&key='+ key +'&order=date&maxResults=20'
        request         = urllib2.Request(channel_url, headers = self.headers)
        response        = urllib2.urlopen(request).read()
        channel_msg     = json.loads(response)
        
        self.channel_video = channel_msg


    def getVideoData(self):
        vide_id = ''
        for item in self.channel_video['items']:
            str_date = item['snippet']['publishedAt']
            try:  
                if(self.comparison(str_date[0:10])):
                    vide_id += item['id']['videoId'] + ','
            except:
                print u'未找到 videoId'

        
        # 获取用户数据
        url         = 'https://www.googleapis.com/youtube/v3/videos'
        video_array = vide_id
        part        = 'snippet,contentDetails,statistics'
        key         = 'AIzaSyBS8p-aiWmI6jeCFvRZTYKgO5AkqrAXmkA'
        video_url   = url + '?id='+video_array+'&part='+part+'&key='+ key
        request     = urllib2.Request(video_url, headers = self.headers)
        response    = urllib2.urlopen(request).read()
        msg         = json.loads(response)

        self.createCsv(msg)

    def comparison(self,str_date):
        st = time.strptime(str_date, '%Y-%m-%d')
        month_day = time.time()
        # 计算剩余天数
        day = int((month_day - time.mktime(st))/(60*60*24))
        judge = True if day<45 else False
       
        return judge

    def createCsv(self,items):
        on_off = True
        car_name = ''
        

        if (items):
            month_day = time.strftime("%m_%d")

            if(self.channel_id == 'UC-MByr5LRnWmVN94Ija4XJg'):
                car_name = 'GO車志'
            if(self.channel_id == 'UC02UVoPQeVi5yVJAMmn1Fmw'):
                car_name = '全名疯車Bar'
            if( self.channel_id == 'UCnao4xB4LPLa6oFLjbOg0oQ' and on_off ):
                car_name = '狂人日誌'
            if( self.channel_id == 'UCKEshOzNkXEAtt6h1zS0A6Q' and on_off ):
                car_name = 'Tcar試車頻道'
            if( self.channel_id == 'UCD76jE_-uOSeyC2q8jX7pVQ' and on_off ):
                car_name = 'carplay'
            if( self.channel_id == 'UCW92-RPH4xq1YqVbON4D0fg' and on_off ):
                car_name = 'ucar'

            djson = {
                "%s"%(car_name):{
                }
            }
            djson["%s"%(car_name)]['subscription'] = self.channel_viewCount

            for item in items['items']:
                djson["%s"%(car_name)][item['id']] = {
                    "title":item['snippet']['title'],
                    "publishedAt":item['snippet']['publishedAt'][0:10],
                    "viewCount":item['statistics']['viewCount'],
                    "commentCount":item['statistics']['commentCount'],
                    "dLikeCount":str(int(item['statistics']['likeCount'])+int(item['statistics']['dislikeCount']))
                }
            # print djson["GO車志-%s"%(self.channel_viewCount)]
            
            

            if os.path.exists('%s/ytb_%s.json'%(self.father_path,month_day)) == False:
                with open('%s/ytb_%s.json'%(self.father_path,month_day),'wb') as f:
                    jStr = json.dumps(djson,ensure_ascii=False)
                    f.write(jStr)
            else:
                fr = open(pjoin('%s/'%(self.father_path), 'ytb_%s.json'%(month_day)))
                model=json.load(fr) 
                for i in djson:
                    model[i] = djson[i]

                with codecs.open('%s/ytb_%s.json'%(self.father_path,month_day),'w') as f:
                   jStr = json.dumps(model,ensure_ascii=False)
                   f.write(jStr)    

    def getJson(self):
        today_time = time.strftime("%m_%d")
        yesterday_time = time.strftime("%m_%d")

        now = int(time.time()) 
        timeStamp = now - 24*60*60
        timeArray = time.localtime(timeStamp)
        otherStyleTime = time.strftime("%m_%d", timeArray)

        yesterday_fr = open(pjoin('%s/'%self.father_path, 'ytb_%s.json'%otherStyleTime))
        yesterday_model = (json.load(yesterday_fr))

        today_fr = open(pjoin('%s/'%self.father_path, 'ytb_%s.json'%today_time))
        today_model = (json.load(today_fr))
        self.model(yesterday_model,today_model)

    def model(self,yesterday_model,today_model):
        for title in today_model:
            try:
                self.wipeoff(yesterday_model[title],today_model[title],title)
            except:
                self.wipeoff('yesterday_model[title]',today_model[title],title)

    def wipeoff(self,y,t,title):
        today_now =[]

        djson = {
            "%s"%(title):{
            }
        }
        
        for tt in t:
            try: 
                djson["%s"%(title)]["%s"%tt] = {
                    "title":t[tt]['title'],
                    "publishedAt":t[tt]['publishedAt'],
                    "viewCount":str(int(t[tt]['viewCount']) - int(y[tt]['viewCount'])),
                    "commentCount": str(int(t[tt]['commentCount']) - int(y[tt]['commentCount'])),
                    "dLikeCount":str(int(t[tt]['dLikeCount']) - int(y[tt]['dLikeCount']))
                }
            except:
                try:
                    djson["%s"%(title)]["%s"%tt] = {
                        "title":t[tt]['title'],
                        "publishedAt":t[tt]['publishedAt'],
                        "viewCount":t[tt]['viewCount'],
                        "commentCount": t[tt]['commentCount'],
                        "dLikeCount":t[tt]['dLikeCount']
                    }
                except:        
                    print tt
        try:
            djson["%s"%(title)]['subscription'] = int(t['subscription']) - int(y['subscription'])
        except:        
            djson["%s"%(title)]['subscription'] = int(t['subscription'])

        self.d_json(djson)

    def d_json(self,data):
        month_day = time.strftime("%m_%d")
        # 写入数据
        if os.path.exists('%s/ytb_%s.csv'%(self.father_path, month_day)) == False:
            with codecs.open('%s/ytb_%s.csv'%(self.father_path, month_day), 'w', 'utf_8_sig') as f:
                f.write('标题,项目,%s'%(month_day))
        
        for title in data:
            with codecs.open('%s/ytb_%s.csv'%(self.father_path, month_day), 'a', 'utf_8') as f:
                f.write('\n\n%s,订阅数,%s\n'%(title,data[title]['subscription']))
            for dd in data[title]:
                with codecs.open('%s/ytb_%s.csv'%(self.father_path, month_day), 'a', 'utf_8') as ff:
                    try:
                        ff.write('\r%s,发布时间,%s\n,观看次数,%s\n,留言数,%s\n,表态,%s\n'%(data[title][dd]['title'],data[title][dd]['publishedAt'],data[title][dd]['viewCount'],data[title][dd]['dLikeCount'],data[title][dd]['commentCount']))
                    except:
                        print u"不存在"


    def sendEmail(self):
        sender = 'ljp@addcn.com'
        passwd = 'Fj0491062'
        receivers = ['wnm@addcn.com','chendachao@addcn.com','ljp@addcn.com']
        month_day = time.strftime("%m_%d")
         
        emails = [elem.strip().split(',') for elem in receivers]
        msg = MIMEMultipart()
        msg['Subject'] = 'ytb_%s.csv'%(month_day)
        msg['From'] = sender
        msg['To'] = ','.join(receivers)
         
        msg.preamble = 'Multipart massage.\n'
        part = MIMEText("YouTube %s 數據"%month_day)
        msg.attach(part)

        part = MIMEApplication(open('%s/ytb_%s.csv'%(self.father_path, month_day),"rb").read())

        part.add_header('Content-Disposition', 'attachment', filename='%s/ytb_%s.csv'%(self.father_path,month_day))
        msg.attach(part)
         
        smtp = smtplib.SMTP("smtp.gmail.com:587")
        smtp.ehlo()
        smtp.starttls()
        smtp.login(sender, passwd)
         
        smtp.sendmail(msg['From'], emails , msg.as_string())
        print 'Send mails to',msg['To']

spider = Youtube_video()

channel_id_array = [
    'UC-MByr5LRnWmVN94Ija4XJg', 
    'UC02UVoPQeVi5yVJAMmn1Fmw',
    'UCnao4xB4LPLa6oFLjbOg0oQ',
    'UCKEshOzNkXEAtt6h1zS0A6Q',
    'UCD76jE_-uOSeyC2q8jX7pVQ',
    'UCW92-RPH4xq1YqVbON4D0fg'
]
for i in channel_id_array:
    spider.start(i)

